<template>
	<view class="animated fadeIn faster">
		<view class="position-fixed w-100 z-index-100 pb-1" :style="{'paddingTop':$store.state.statusBarHeight+'px','backgroundColor':bgcolor}">
			<view class="px-3 d-flex d-flex-between d-flex-middle" :style="{'height': $store.state.titleBarHeight + 'px'}">
				<text class="icon-31fanhui1 iconfont" @click="goBack"></text>
				<text class="d-flex-item d-flex d-flex-center">好友列表</text>
				<text class="iconfont icon-31fanhui1" style="color: #f8f8f8;"></text>
			</view>
		</view>
		<view :style="{paddingTop:$store.state.statusBarHeight+'px',height: $store.state.titleBarHeight + 'px'}"></view>
		<view class="px-3 py-3">
			<view @click="goChatAdministrator" class="d-flex bg-fff p-2 mb-3 rounded15 d-flex-middle d-flex-between">
				<view class="d-flex d-flex-middle">
					<image class="rounded mr-2" src="https://default-imges.oss-cn-beijing.aliyuncs.com/icon/myuser.png" style="width: 110rpx;height: 110rpx;border: solid 1px #f8f8f8;"></image>
					<view class="d-flex">
						<view class="mb font-500">小嗒客服</view>
					</view>
				</view>
			</view>
			<view @click="goChat(friend.profile)" class="d-flex bg-fff p-2 mb-3 rounded15 d-flex-middle d-flex-between" v-for="(friend,cindex) in friendList" :key="cindex">
				<view class="d-flex d-flex-middle">
					<image class="rounded mr-2" :src="friend.profile.avatar" style="width: 110rpx;height: 110rpx;border: solid 1px #f8f8f8;"></image>
					<view class="d-flex">
						<view class="mb font-500">{{friend.profile.nick || '嗒友'}}</view>
					</view>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	import store from '@/store/index.js';
	import { manner } from "@/util/manner.js";
	import {dateUtils} from '@/util/util.js';
	export default {
		data() {
			return {
				bgcolor:'transparent',
				friendList:[],
				conversationList:[],
			}
		},
		onPageScroll(e) {
			let scrollTop = e.scrollTop
			if (scrollTop > 10) {
				this.bgcolor = '#ffffff'
			}else{
				this.bgcolor = 'transparent'
			}
		},
		computed:{
			getDateString(){
				return function fn(value){
					let now = new Date().getTime();
					let old = value
					let	oldTime = 0
					if (typeof value === 'number') {
						oldTime = old*1000;
					}
					if (typeof value === 'string') {
						oldTime = new Date(value.replace(/-/g, '/')).getTime();
					}
					return dateUtils.humanize(now - oldTime);
				}
			}
			
		},
		onReady() {
			this.getFriendList()
		},
		methods: {
			goBack(){
				manner.goBack()
			},
			myFriend(){
				uni.navigateTo({
					url:'/message/friend/friend'
				})
			},
			goChatAdministrator(){
				const item = {
					id:'122',
					name:'小嗒客服',
					avatar:'https://default-imges.oss-cn-beijing.aliyuncs.com/icon/myuser.png',
					conversationID:'C2C122',
					type:'C2C'
				}
				uni.navigateTo({
					url:'/message/im/im?to=' + encodeURIComponent(JSON.stringify(item))
				})
			},
			goChat(profile){
				console.log('userProfile',profile)
				const item = {
					id:profile.userID,
					name:profile.nick,
					avatar:profile.avatar,
					conversationID:'C2C'+profile.userID,
					type:'C2C'
				}
				uni.navigateTo({
					url:'/message/im/im?to=' + encodeURIComponent(JSON.stringify(item))
				})
			},
			getFriendList(){
				let that = this
				that.$tim.getFriendList().then(function(imResponse) {
					console.log('getFriendList',imResponse.data)
					if(imResponse.data.length>0){
						that.friendList = imResponse.data
					}
				}).catch(function(imError) {
					console.warn('getFriendList error:', imError);
				});
				
				// tim.getFriendList().then((imResponse) => {
				// 	if (imResponse.code == 0) {
				// 		store.commit('SET_FRIENDLIST', imResponse.data);
				// 	}
				// }).catch((imError) => {
				// 	console.warn('getFriendList error:', imError);
				// });
			},
			getConversationList(){
				let that = this
				that.$tim.getConversationList().then(function(imResponse) {
					that.conversationList = imResponse.data.conversationList
					console.log('conversationList',that.conversationList)
				}).catch(function(imError) {
					console.warn('获取会话列表失败的相关信息:', imError);
				});
			},
			openIm(){
				let item = {
					id: 'hellowk',
					name: 'hellowk',
					avatar: 'https://gameplus-platform.cdn.bcebos.com/gameplus-platform/upload/file/img/ed39ad04e7655cbea70fe406f70a7fe8/ed39ad04e7655cbea70fe406f70a7fe8.png',
					conversationID: 'C2Chellowk',
					type: 'C2C'
				}
				let iitem = {
					id: 'weCat',
					name: 'weCat',
					avatar: 'https://gameplus-platform.cdn.bcebos.com/gameplus-platform/upload/file/img/ed39ad04e7655cbea70fe406f70a7fe8/ed39ad04e7655cbea70fe406f70a7fe8.png',
					conversationID: 'C2CweCat',
					type: 'C2C'
				}
				uni.navigateTo({
					url:'/message/im/im?to=' + encodeURIComponent(JSON.stringify(iitem))
				})
			},
			gotoCardiacSigin() {
				uni.navigateTo({
					url:'/pagesMain/mes-CardiacSignal/mes-CardiacSignal'
				})
			},
			openHomePage() {
				uni.navigateTo({
					url:'/pagesMain/HomePage/HomePage'
				})
			},
		}
	}
</script>

<style scoped>
.blueSrch{
	background: #CFF0FB;
	border: 1px solid #46C4FF;
	color: #46C4FF;
	height: 60rpx;
	border-radius: 30rpx;
}
</style>
